﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Drawing.Imaging;
using DevExpress.XtraCharts;
using System.Data;

namespace FinallyVideoCaptureTest
{
    public partial class StatisticsForm : Form
    {
        public StatisticsForm()
        {
            InitializeComponent();
        }

        private void StatisticsForm_Load(object sender, EventArgs e)
        {
            this.rabHour.Checked = true;
            this.chartControl1.Series[0].Points.Clear();
        }
        private IList<Frame> ToShowFrames = new List<Frame>();

        private string dbPath = "TestFrames.db4o";

        //查询事件
        private void btnShowResult_Click(object sender, EventArgs e)
        {
            DateTime sTime = DateTime.Parse(dtpSTime.Text);
            DateTime eTime = DateTime.Parse(dtpETime.Text);

            if (sTime.CompareTo(eTime) > 0)
            {
                MessageBox.Show("起始时间要小于或等于结束时间！");
                return;
            }
            Db4oProvider db4Provider = new Db4oProvider(dbPath);
            db4Provider.StartServer();

            if (sTime.CompareTo(eTime) == 0)
            {
                eTime = eTime.AddHours(23);
                eTime = eTime.AddMinutes(59);
                eTime = eTime.AddSeconds(59);
            }
            IList<Frame> frames = db4Provider.GetFrames(sTime, eTime);
            BindDataToChartControl(frames.ToList<Frame>(), sTime, eTime);
           
            db4Provider.StopServer();
        }

        //Frame f1 = new Frame();
        //f1.PersonCount = 330;
        //f1.PicSavePath = @"D:\TEST";
        //f1.CaptureTime = DateTime.Now;

        //Frame f2 = new Frame();
        //f2.PersonCount = 440;
        //f2.PicSavePath = @"D:\TEST";
        //f2.CaptureTime = DateTime.Now;
        //db4Provider.SaveFrame(f1);
        //db4Provider.SaveFrame(f2);

        private string statisticsType = string.Empty;
        private void rabDay_Click(object sender, EventArgs e)
        {
            if (sender != null)
            {
                RadioButton rab = (RadioButton)sender;
                rab.Checked = true;
                statisticsType = string.Format("{0}统计", rab.Text);
            }
        }

        private void BindDataToChartControl(List<Frame> frames, DateTime sTime, DateTime eTime)
        {
         
            if (sTime.Day == eTime.Day)
            {
                ShowHourModel(frames, sTime);
            }
            else
            {
                ShowDayModel(frames, sTime, eTime);
            }
        }

        //显示每小时的人数统计
        private void ShowHourModel(List<Frame> frames, DateTime sTime)
        {
            DateTime eTemp = sTime.AddDays(1);

            var list = new Dictionary<DateTime, int>();
            var current = sTime;
            while (true)
            {
                list.Add(current, 0);
                current = current.AddHours(1);
                if (current >= eTemp)
                    break;
            }

            var res = from f in frames
                      group f by f.CaptureTime.Date.AddHours(f.CaptureTime.Hour)
                      into g
                      orderby g.Key ascending
                      select new TimeCount { Time = g.Key, Count = g.Sum(item => item.PersonInCount+item.PersonOutCount) };
            int count = 0;
            foreach (var g in res)
            {
                list[g.Time] = g.Count;
                count += g.Count;
            }
            this.chartControl1.Series[0].LegendText = string.Format("人口总数:{0}", count.ToString());
            this.chartControl1.Series[0].Points.Clear();
            list.Select(l => this.chartControl1.Series[0].Points.Add(new SeriesPoint(l.Key.Hour.ToString() + "点", l.Value))).ToArray();
        }

        //显示每天的人数统计
        private void ShowDayModel(List<Frame> frames, DateTime sTime, DateTime eTime)
        {
            DateTime sTemp = sTime;
            DateTime eTemp = eTime.AddDays(1);

            var list = new Dictionary<DateTime, int>();
            var current = sTime;
            while (true)
            {
                list.Add(current, 0);
                current = current.AddDays(1);
                if (current >= eTemp)
                    break;
            }

            var res = from f in frames
                      group f by f.CaptureTime.Date
                          into g
                          orderby g.Key ascending
                          select new TimeCount { Time = g.Key, Count = g.Sum(item => item.PersonInCount + item.PersonOutCount) };
            int count = 0;
            foreach (var g in res)
            {
                list[g.Time] = g.Count;
                count += g.Count;
            }
            this.chartControl1.Series[0].LegendText = string.Format("人口总数:{0}", count.ToString());

            this.chartControl1.Series[0].Points.Clear();
            list.Select(l => this.chartControl1.Series[0].Points.Add(new SeriesPoint(l.Key.ToShortDateString(), l.Value))).ToArray();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {

        }
    }
}

//  //splitting time up into hour intervals
//fooList.GroupBy(f => f.Time.Date.AddHours(f.Time.Hour))

//// splitting time up into day intervals
//fooList.GroupBy(f => f.Time.Date)

// //splitting time up into month intervals
//fooList.GroupBy(f => f.Time.Date.AddDays(-f.Time.Day))


//SetTable(ref dataTb1);
//se1.DataSource = dataTb1;
////X轴的数据字段

//se1.ArgumentDataMember = "Month";

////Y轴的数据字段

//se1.ValueDataMembers[0] = "GongZi";

//this.chartControl1.Series.Add(se1);